package com.example.jsoupdemo.util;

import com.example.jsoupdemo.beans.Constants;
import com.example.jsoupdemo.beans.ProxyIp;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 设置user-agent,获取document
 */
public class DocumentUtil {
    final static Logger logger = LoggerFactory.getLogger(ProxyIpUtil.class);
    public static Document againGetDocument(String url){
        Connection connection = Jsoup.connect(url);
        //设置user-agent
        connection.userAgent(Constants.UserAgent.Chrome);
        //设置动态代理ip返回Document类型的对象
        Document document=null;
        try {
            //获取动态代理ip
            ProxyIp proxyIp = ProxyIpUtil.getProxyIp();
            //设置代理ip
            //connection.proxy(proxyIp.getIp(),Integer.parseInt(proxyIp.getPort()));
            logger.info(proxyIp.getIp()+"--------"+Integer.parseInt(proxyIp.getPort()));
            //获取Documnet
            document = connection.get();
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("建立连接异常>>>>>>>>3秒后继续连接");
            try {
                Thread.sleep(3000); //休眠3秒
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
            return againGetDocument(url);
        }
        return document;
    }

}
